package org.moka.ioio.ioiopilot.gui;


import org.moka.ioio.ioiopilot.IOIOThread;

import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;

public class GraphViewThread extends Thread {
	private GraphSurface mGraphSurface;
	private SurfaceHolder mHolder;
	private boolean mRun = false;
	private int cont;

	public GraphViewThread(GraphSurface panel) {
		mGraphSurface = panel;
		mHolder = mGraphSurface.getHolder();
	}

	public void setRunning(boolean run) {
		mRun = run;
	}
 
	@Override
	public void run() {
		Canvas canvas = null;
		while (mRun)
		{
			if(cont%2==0)
			{
				mGraphSurface.doUpdate();
			}
			cont++;
			if (mGraphSurface.shouldRedraw()) {
				canvas = mHolder.lockCanvas();
				if (canvas != null) {
					mGraphSurface.doDraw(canvas);
					mHolder.unlockCanvasAndPost(canvas);
				}

			}
			try {
				sleep(40);
			} catch (InterruptedException e) {
				Log.e(IOIOThread.TAG, e.getMessage());
				e.printStackTrace();
			}
		}
	}
}